package com.sangfor.ssl.service.line;

import android.os.Handler;
import android.os.HandlerThread;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.line.LineSelectionTask;
import com.sangfor.ssl.service.netmonitor.NetworkBean;
import com.sangfor.ssl.service.netmonitor.NetworkMonitor;
import java.util.concurrent.ConcurrentHashMap;

/* loaded from: classes2.dex */
public class LineSelectManager implements LineSelectionTask.Callback {
    private static final String RECONNECT_THREAD = "RECONNECT_THREAD";
    private static final String TAG = "LineSelectManager";
    private boolean inited;
    private Handler mReconnectHandler;
    private ConcurrentHashMap<LineSelectionTask.TaskType, LineSelectionTask> mSelectTaskMap;

    /* synthetic */ LineSelectManager(AnonymousClass1 anonymousClass1) {
        this();
    }

    /* loaded from: classes2.dex */
    private static class SingletonHolder {
        private static final LineSelectManager INSTANCE = new LineSelectManager(null);

        private SingletonHolder() {
        }
    }

    public static final LineSelectManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private LineSelectManager() {
        this.mReconnectHandler = null;
        this.inited = false;
        if (0 == 0) {
            this.mSelectTaskMap = new ConcurrentHashMap<>();
            initReconnectThread();
            this.inited = true;
        }
    }

    @Override // com.sangfor.ssl.service.line.LineSelectionTask.Callback
    public void onSelectLineSuccess(LineResult lineResult, boolean z, UserTaskInfo userTaskInfo) {
        Log.debug(TAG, "select line success, and this line changed:" + z);
        removeSelectTask(userTaskInfo.type);
        userTaskInfo.callback.onSelectLineSuccess(lineResult, z);
    }

    @Override // com.sangfor.ssl.service.line.LineSelectionTask.Callback
    public void onSelectLineFailed(int i, UserTaskInfo userTaskInfo) {
        Log.error(TAG, "select line failed, error:" + i);
        removeSelectTask(userTaskInfo.type);
        userTaskInfo.callback.onSelectLineFailed(i);
    }

    public boolean doLineSelectTask(String str, LineSelectionTask.TaskType taskType, SelectLineCallback selectLineCallback, Handler handler) {
        if (taskType == null || TextUtils.isEmpty(str) || selectLineCallback == null) {
            throw new IllegalArgumentException("inputUrl/callback cannot be empty/null");
        }
        Log.info(TAG, "start line selection, task type:" + taskType);
        NetworkBean buildNetworkInformation = NetworkMonitor.getInstance().buildNetworkInformation();
        if (buildNetworkInformation == null) {
            return false;
        }
        int i = AnonymousClass1.$SwitchMap$com$sangfor$ssl$service$line$LineSelectionTask$TaskType[taskType.ordinal()];
        LineSelectionTask lineSelectionTask = this.mSelectTaskMap.get(taskType);
        if (lineSelectionTask != null) {
            Log.warn(TAG, "Reduplicate line selection task.");
            lineSelectionTask.cancel();
            removeSelectTask(taskType);
        }
        LineSelectionTask lineSelectionTask2 = new LineSelectionTask(new UserTaskInfo(str, taskType, selectLineCallback, handler), this, buildNetworkInformation);
        lineSelectionTask2.start();
        putSelectTask(taskType, lineSelectionTask2);
        return true;
    }

    /* renamed from: com.sangfor.ssl.service.line.LineSelectManager$1  reason: invalid class name */
    /* loaded from: classes2.dex */
    static /* synthetic */ class AnonymousClass1 {
        static final /* synthetic */ int[] $SwitchMap$com$sangfor$ssl$service$line$LineSelectionTask$TaskType;

        static {
            int[] iArr = new int[LineSelectionTask.TaskType.values().length];
            $SwitchMap$com$sangfor$ssl$service$line$LineSelectionTask$TaskType = iArr;
            try {
                iArr[LineSelectionTask.TaskType.CONNECT.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$sangfor$ssl$service$line$LineSelectionTask$TaskType[LineSelectionTask.TaskType.NETWORK_CHANGE.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$com$sangfor$ssl$service$line$LineSelectionTask$TaskType[LineSelectionTask.TaskType.LOGIN_FAILED.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            try {
                $SwitchMap$com$sangfor$ssl$service$line$LineSelectionTask$TaskType[LineSelectionTask.TaskType.CONNECT_FAILED.ordinal()] = 4;
            } catch (NoSuchFieldError unused4) {
            }
            try {
                $SwitchMap$com$sangfor$ssl$service$line$LineSelectionTask$TaskType[LineSelectionTask.TaskType.OTHER.ordinal()] = 5;
            } catch (NoSuchFieldError unused5) {
            }
        }
    }

    public void doCancelLineTask(LineSelectionTask.TaskType taskType) {
        LineSelectionTask lineSelectionTask = this.mSelectTaskMap.get(taskType);
        if (lineSelectionTask != null) {
            lineSelectionTask.cancel();
            removeSelectTask(taskType);
        }
    }

    private void putSelectTask(LineSelectionTask.TaskType taskType, LineSelectionTask lineSelectionTask) {
        this.mSelectTaskMap.put(taskType, lineSelectionTask);
    }

    private void removeSelectTask(LineSelectionTask.TaskType taskType) {
        this.mSelectTaskMap.remove(taskType);
    }

    private void initReconnectThread() {
        HandlerThread handlerThread = new HandlerThread(RECONNECT_THREAD);
        handlerThread.start();
        this.mReconnectHandler = new Handler(handlerThread.getLooper());
    }

    public void postReconnectTask(Runnable runnable) {
        if (runnable == null) {
            return;
        }
        this.mReconnectHandler.post(runnable);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes2.dex */
    public static class UserTaskInfo {
        SelectLineCallback callback;
        Handler handler;
        String inputUrl;
        LineSelectionTask.TaskType type;

        UserTaskInfo(String str, LineSelectionTask.TaskType taskType, SelectLineCallback selectLineCallback, Handler handler) {
            this.inputUrl = str;
            this.type = taskType;
            this.callback = selectLineCallback;
            this.handler = handler;
        }
    }
}
